package com.gdssoft.pdaweb.dao.am.impl;
/*
 *  捷达世软件（深圳）有限公司
 *  Copyright (C) 2008-2010 ShenZhen JieDaShi Software Limited Company.
*/
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.gdssoft.core.Constants;
import com.gdssoft.core.dao.impl.BaseDaoImpl;
import com.gdssoft.pdaweb.dao.am.PdaAmMeterDao;
import com.gdssoft.pdaweb.model.am.PdaAmMeter;

public class PdaAmMeterDaoImpl extends BaseDaoImpl<PdaAmMeter> implements PdaAmMeterDao{

	public PdaAmMeterDaoImpl() {
		super(PdaAmMeter.class);
	}

	@Override
	public PdaAmMeter getByAmId(String amId) {
		List<PdaAmMeter> list = new ArrayList<PdaAmMeter>();
		try{
			list = findByHql("from PdaAmMeter pm where pm.amId='"+amId+"'");
		}catch(Exception e){
			logger.debug("PdaAmMeterList query error:"+e.getMessage());			
		}
		return list.size()>0?list.get(0):new PdaAmMeter();
	}

	@Override
	public List<PdaAmMeter> getAllForExcel() {
		List<PdaAmMeter> list = new ArrayList<PdaAmMeter>();
		String sql = "SELECT A.AM_ID,A.DISTRICT_ID,A.BUILDING_ID,pa.NAME FROM (" +
				" SELECT pm.AM_ID,pm.DISTRICT_ID,pm.BUILDING_ID,pm.APARMENT_ID" +
				" FROM PDA_AM_METER pm WHERE pm.TYPE = 2" +
//				" UNION" +
//				" SELECT po.AM_ID,pm.DISTRICT_ID,pm.BUILDING_ID,pm.APARMENT_ID " +
//				" FROM PDA_AM_METER pm " +
//				" LEFT JOIN PDA_AM_OPERATE po on pm.AM_ID=po.OLD_AM_ID " +
//				" WHERE pm.TYPE = 3 and pm.IS_ALERT=1 " +
				") A JOIN PDA_APARMENT pa ON A.APARMENT_ID=pa.APARMENT_ID";
		try{
			List rows = jdbcTemplate.queryForList(sql);
			Iterator it = rows.iterator();
			while(it.hasNext()) {
			    Map map = (Map) it.next();
			    PdaAmMeter newpm = new PdaAmMeter();
			    newpm.setAmId(map.get("AM_ID").toString());
			    newpm.setDistrictId(map.get("DISTRICT_ID").toString());
			    newpm.setBuildingId(map.get("BUILDING_ID").toString());
			    newpm.setAparmentName(map.get("NAME").toString());
			    list.add(newpm);
			}
		}catch(Exception e){
			logger.debug("PdaAmMeterList query error:"+e.getMessage());			
		}
		
		return list;
	}

	@Override
	public List<PdaAmMeter> getByPosition(String districtId, String buildingId) {
		List<PdaAmMeter> list = new ArrayList<PdaAmMeter>();
		try{
			list = findByHql("from PdaAmMeter pm where pm.isPlanned='"+Constants.UNPLANNED+"' and pm.districtId='"+districtId+"' and pm.buildingId='"+buildingId+"'");
		}catch(Exception e){
			logger.debug("PdaWaterMeter query error:"+e.getMessage());			
		}
		return list;
	}
	@Override
	public List<PdaAmMeter> getByHouseId(String houseId){
		String hql = "from PdaAmMeter pam where pam.aparmentId=?";
		return this.findByHql(hql,new String[]{houseId});
	}
	@Override
	public PdaAmMeter getByBarcode(String barcode){
		String hql = "from PdaAmMeter pam where pam.pdaMdAssetInfo.barCode=?";
		return (PdaAmMeter)findUnique(hql,new String[]{barcode});
	}

	@Override
	public boolean isCrmMeterNoExist(String crmMeterNo) {
		//return findByHql("from PdaAmMeter p where p.crmMeterNo=?",new Object[]{crmMeterNo}).size()>0;
		return false;
	}

}